// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import { NotificationCenter } from "../utils/NotificationCenter";

import {
    commonPost,
    day7InfoUrl,
    day7PrizeUrl,
    getAdGuideUrl,
    finishAdUrl,
    tixianUrl,
    rmbHistoryHttp,
    pmdUrl,
    worldRankUrl,
    bindWxUrl
} from "../utils/CommonUtils";

import {
    L2EProtocol,
    S2CProtocol,
    C2S_7DAY_INFO,
    S2C_7DAY_INFO,
    C2S_7DAY_PRI,
    S2C_7DAY_PRI,
    C2S_REQ_AD_CODE,
    S2C_REQ_AD_CODE,
    C2S_FINISH_AD,
    S2C_FINISH_AD,
    C2S_TIXIAN,
    S2C_TIXIAN,
    C2S_RMB_HISTORY,
    S2C_RMB_HISTORY,
    C2S_PMD,
    C2S_WORLD_RANK,
    L2E_WORLD_RANK,
    C2S_BIND_WX, S2C_BIND_WX
} from "./ELProtocol";

const {ccclass, property} = cc._decorator;

@ccclass
export default class HallProto extends cc.Component {

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        NotificationCenter.Instance.on("bindWxHandler", this.bindWxHandler, this);
        NotificationCenter.Instance.on("worldRankHandle", this.worldRankHandle, this);
        NotificationCenter.Instance.on("day7InfoHandle", this.day7InfoHandle, this);
        NotificationCenter.Instance.on("day7PrizeHandle", this.day7PrizeHandle, this);
        NotificationCenter.Instance.on("getAdGuideHandle", this.getAdGuideHandle, this);
        NotificationCenter.Instance.on("finishAdHandle", this.finishAdHandle, this);
        NotificationCenter.Instance.on("tixianHandler", this.tixianHandler, this);
        NotificationCenter.Instance.on("rmbHistoryHandler", this.rmbHistoryHandler, this);
        NotificationCenter.Instance.on("pmdHandler", this.pmdHandler, this);
        
        NotificationCenter.Instance.on("requestBindWx", this.requestBindWx, this);
        NotificationCenter.Instance.on("requestworldRank", this.requestworldRank, this);
        NotificationCenter.Instance.on("request7Day", this.request7Day, this);
        NotificationCenter.Instance.on("request7DayPrize", this.request7DayPrize, this);
        NotificationCenter.Instance.on("requestFinishAd", this.requestFinishAd, this);
        NotificationCenter.Instance.on("requestAdGuide", this.requestAdGuide, this);
        
        NotificationCenter.Instance.on("requestTiXian", this.requestTiXian, this);
        NotificationCenter.Instance.on("requestRMBHistory", this.requestRMBHistory, this);
        NotificationCenter.Instance.on("requestPMD", this.requestPMD, this);
        
    }
    
    onDestroy() {
        NotificationCenter.Instance.off("bindWxHandler", this.bindWxHandler, this);
        NotificationCenter.Instance.off("worldRankHandle", this.worldRankHandle, this);
        NotificationCenter.Instance.off("day7InfoHandle", this.day7InfoHandle, this);
        NotificationCenter.Instance.off("day7PrizeHandle", this.day7PrizeHandle, this);
        NotificationCenter.Instance.off("getAdGuideHandle", this.getAdGuideHandle, this);
        NotificationCenter.Instance.off("finishAdHandle", this.finishAdHandle, this);
        NotificationCenter.Instance.off("tixianHandler", this.tixianHandler, this);
        NotificationCenter.Instance.off("rmbHistoryHandler", this.rmbHistoryHandler, this);
        NotificationCenter.Instance.off("pmdHandler", this.pmdHandler, this);

        NotificationCenter.Instance.on("requestBindWx", this.requestBindWx, this);
        NotificationCenter.Instance.off("requestworldRank", this.requestworldRank, this);
        NotificationCenter.Instance.off("request7Day", this.request7Day, this);
        NotificationCenter.Instance.off("request7DayPrize", this.request7DayPrize, this);
        NotificationCenter.Instance.off("requestFinishAd", this.requestFinishAd, this);
        NotificationCenter.Instance.off("requestAdGuide", this.requestAdGuide, this);

        NotificationCenter.Instance.off("requestTiXian", this.requestTiXian, this);
        NotificationCenter.Instance.off("requestRMBHistory", this.requestRMBHistory, this);
        NotificationCenter.Instance.off("requestPMD", this.requestPMD, this);
        
    }
    
    start () {

    }
    
    private handleError(response: any) {
        if (response.code != 200) {
            console.log("服务器错误提示："+response.msg);

            if (response.code == 542) {
                // 凭证失效,需要微信拿code
                NotificationCenter.Instance.emit(L2EProtocol.l2e_req_wx_login);
            }
            return true;
        }

        return false;
    }
    
    private requestworldRank(type: number) {
        let data = <C2S_WORLD_RANK>{};
        let logicCenter = cc.find('LogicCenterNode').getComponent('LogicCenter');
        let user = logicCenter.getUserInfo();
        data.token = user.token;
        data.type = type;
        commonPost(worldRankUrl, "worldRankHandle", data);
    }

    private worldRankHandle(response: string) {
        let resp = JSON.parse(response);
        if (this.handleError(resp) == true) {
            cc.warn("通用错误处理");
            return;
        }

        let updateData = <L2E_WORLD_RANK>{};
        updateData.rankListData = resp.data;
        NotificationCenter.Instance.emit(L2EProtocol.l2e_refresh_rank, updateData);
    }
    public request7Day() {
        let data = <C2S_7DAY_INFO>{};
        let logicCenter = cc.find('LogicCenterNode').getComponent('LogicCenter');
        let user = logicCenter.getUserInfo();
        data.token = user.token;
        commonPost(day7InfoUrl, "day7InfoHandle", data);
    }
    
    public day7InfoHandle(response: string) {
        let resp = JSON.parse(response);
        if (this.handleError(resp) == true) {
            cc.warn("通用错误处理");
            return;
        }
        
        let updateData: S2C_7DAY_INFO = <S2C_7DAY_INFO>{};
        updateData.prizeDay = resp.data.prizeDay;
        updateData.hasTake = resp.data.hasTake;
        NotificationCenter.Instance.emit(S2CProtocol.s2c_7day_info, updateData);
    }
    
    private request7DayPrize() {
        let data = <C2S_7DAY_PRI>{};
        let logicCenter = cc.find('LogicCenterNode').getComponent('LogicCenter');
        let user = logicCenter.getUserInfo();
        data.token = user.token;
        commonPost(day7PrizeUrl, "day7PrizeHandle", data);
    }
    
    private day7PrizeHandle(response: string) {
        let resp = JSON.parse(response);
        if (this.handleError(resp) == true) {
            cc.warn("通用错误处理");
            return;
        }
        
        let updateData: S2C_7DAY_PRI = <S2C_7DAY_PRI>{};
        updateData.gunId = resp.data.gunId;
        updateData.gunPos = resp.data.gunPos;
        updateData.diamonds = resp.data.diamonds;
        updateData.adddiamonds = resp.data.adddiamonds;
        updateData.coin = resp.data.coin;
        updateData.addcoin = resp.data.addcoin;
        updateData.gunWillBuy = resp.data.gunWillBuy;
        NotificationCenter.Instance.emit(S2CProtocol.s2c_7day_pri, updateData);
        
    }

    private requestAdGuide(adCode: number, adGuideData: number = 0, type: number  = 0) {
        let data = <C2S_REQ_AD_CODE>{};
        let logicCenter = cc.find('LogicCenterNode').getComponent('LogicCenter');
        let user = logicCenter.getUserInfo();
        data.token = user.token;
        data.adGuideCode = adCode;
        data.adGuideData = adGuideData;
        data.type = type;
        commonPost(getAdGuideUrl, "getAdGuideHandle", data);
    }

    private getAdGuideHandle(response: string) {
        let resp = JSON.parse(response);
        if (this.handleError(resp) == true) {
            cc.warn("通用错误处理");
            return;
        }

        let updateData: S2C_REQ_AD_CODE = <S2C_REQ_AD_CODE>{};
        updateData.adGuideCode = resp.data.adGuideCode;

        // updateData.adMultNum = resp.data.adMultNum;
        // updateData.adRotaryNum = resp.data.adRotaryNum;
        // updateData.sharedNum = resp.data.sharedNum;
        // updateData.adType = resp.data.type;

        NotificationCenter.Instance.emit(S2CProtocol.s2c_req_adcode, updateData);
    }

    private requestFinishAd() {
        let data = <C2S_FINISH_AD>{};
        let logicCenter = cc.find('LogicCenterNode').getComponent('LogicCenter');
        let user = logicCenter.getUserInfo();
        data.token = user.token;
        commonPost(finishAdUrl, "finishAdHandle", data);
    }

    private finishAdHandle(response: string) {
        let resp = JSON.parse(response);
        if (this.handleError(resp) == true) {
            cc.warn("通用错误处理");
            return;
        }

        let updateData: S2C_FINISH_AD = <S2C_FINISH_AD>{};
        updateData.finishdAdCode = resp.data.finishdAdCode;
        updateData.trmb = resp.data.trmb;
        updateData.addtrmb = resp.data.addtrmb;
        
        updateData.adNum = resp.data.adNum;
        updateData.tfAdNum = resp.data.tfAdNum;
        
        updateData.serverTime = resp.serverTime;
        NotificationCenter.Instance.emit(S2CProtocol.s2c_finish_ad, updateData);
    }

    private requestTiXian(amount: number) {
        let data = <C2S_TIXIAN>{};
        let logicCenter = cc.find('LogicCenterNode').getComponent('LogicCenter');
        let user = logicCenter.getUserInfo();
        data.token = user.token;
        data.amount = amount;
        commonPost(tixianUrl, "tixianHandler", data);
    }

    private tixianHandler(response: string) {
        let resp = JSON.parse(response);
        if (this.handleError(resp) == true) {
            cc.warn("通用错误处理");
            if (resp.code != 542) {
                NotificationCenter.Instance.emit(L2EProtocol.l2e_tixian_error, resp.code, resp.msg);
            }
            return;
        }

        let updateData = <S2C_TIXIAN>{};
        updateData.rmb = resp.data.rmb;
        updateData.addrmb = resp.data.addrmb;
        NotificationCenter.Instance.emit(S2CProtocol.s2c_tixian, updateData);
        NotificationCenter.Instance.emit("requestPMD");
    }

    private requestRMBHistory(page: number) {
        let data = <C2S_RMB_HISTORY>{};
        let logicCenter = cc.find('LogicCenterNode').getComponent('LogicCenter');
        let user = logicCenter.getUserInfo();
        data.token = user.token;
        data.page = page;
        data.size = 10;
        commonPost(rmbHistoryHttp, "rmbHistoryHandler", data);
    }

    private rmbHistoryHandler(response: string) {
        let resp = JSON.parse(response);
        if (this.handleError(resp) == true) {
            cc.warn("通用错误处理");
            return;
        }

        let updateData = <S2C_RMB_HISTORY>{};
        updateData.total = resp.data.total;
        updateData.data = resp.data.rows;
        NotificationCenter.Instance.emit(S2CProtocol.s2c_rmb_list, updateData);
    }

    private requestPMD() {
        let data = <C2S_PMD>{};
        let logicCenter = cc.find('LogicCenterNode').getComponent('LogicCenter');
        let user = logicCenter.getUserInfo();
        data.token = user.token;
        commonPost(pmdUrl, "pmdHandler", data);
    }

    private pmdHandler(response: string) {
        try {
            let resp = JSON.parse(response);
            if (this.handleError(resp) == true) {
                cc.warn("通用错误处理");
                return;
            }


            NotificationCenter.Instance.emit(S2CProtocol.s2c_pmd_list, resp.data);
        } catch (error) {
            console.log(error);
            console.log(response);
        }
        let self = this;
        cc.tween(this.node)
            .delay(300)
            .call(() => { self.requestPMD() })
            .start();
    }

    private requestBindWx(code: string) {
        let data = <C2S_BIND_WX>{};
        let logicCenter = cc.find('LogicCenterNode').getComponent('LogicCenter');
        let user = logicCenter.getUserInfo();
        data.token = user.token;
        data.code = code;
        commonPost(bindWxUrl, "bindWxHandler", data);
    }

    private bindWxHandler(response: string) {
        try {
            let resp = JSON.parse(response);
            if (this.handleError(resp) == true) {
                cc.warn("通用错误处理");
                return;
            }

            let data = <S2C_BIND_WX>{};
            data.nickName = resp.data.nickName;
            data.headIcon = resp.data.headIcon;
            NotificationCenter.Instance.emit(S2CProtocol.s2c_bind_wx, data);
        } catch (error) {
            console.log(error);
            console.log(response);
        }
    }

}
